import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:meePet/app/RouteConstants.dart';
import 'package:meePet/base/BaseActivity.dart';
import 'package:meePet/base/BaseFragment.dart';
import 'package:meePet/manager/RouteManager.dart';
import 'package:meePet/manager/UserManager.dart';
import 'package:meePet/utils/ext/SizeExt.dart';
import 'package:meePet/utils/ext/StringExt.dart';

import '../../app/LocalColor.dart';
import '../../base/LiveData.dart';
import '../../bean/FoundTabListData.dart';
import '../../bean/RolePageData.dart';
import '../../utils/LogUtils.dart';
import 'RoleListFragmentVM.dart';
import 'item/CustomRoleItem.dart';
import 'item/OfficialRoleItem.dart';

class RoleListFragment extends BaseFragment {
  final FoundTab? _data;

  RoleListFragment(this._data);

  @override
  State<StatefulWidget> createState() {
    return RoleListFragmentState(_data);
  }
}

class RoleListFragmentState extends BaseFragmentState {
  RoleListFragmentVM? viewModel;
  final FoundTab? _data;
  List<RoleData>? roleList = [];
  bool firstLoad = false;
  late OfficialItem officialItem;
  late CustomRoleItem customRoleItem;

  RoleListFragmentState(this._data);

  @override
  void onCreate() {
    String tagStr = getClassName(context) + _data!.alias.orEmpty();
    LogUtils.i("本类的hash code是:${tagStr}");
    viewModel = Get.put(RoleListFragmentVM(), tag: "${tagStr}");
    officialItem = OfficialItem();
    customRoleItem = CustomRoleItem();
    observerToRefreshUI();
    viewModel?.curTab = _data;

    if (!firstLoad) {
      firstLoad = true;
      viewModel?.getRoleListByCondition();
    }

    super.onCreate();
  }

  @override
  void onResume() {
    super.onResume();
    if (!firstLoad) {
      firstLoad = true;
      viewModel?.getRoleListByCondition();
    }
  }

  @override
  Widget initView(BuildContext context) {
    return _rootLayout();
  }

  Widget _rootLayout() {
    return Stack(alignment: Alignment.topCenter, children: [
      const SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: DecoratedBox(
            decoration: BoxDecoration(color: LocalColor.colorTheme),
          )),
      Container(
        padding: EdgeInsets.only(
          left: 20.sw(),
          right: 20.sw(),
        ),
        child: _getContentView(),
      ),
    ]);
  }

  Widget _getContentView() {
    List<Widget> viewList = [];
    int index = 0;
    roleList?.forEach((element) {
      viewList.add(getItemView(index, element));
      index += 1;
    });
    return MediaQuery.removePadding(
      removeTop: true,
      context: context,
      child: ListView(
        children: viewList,
      ),
    );
  }

  Widget _panelBg(Widget content) {
    return Stack(
      children: [
        Container(
          padding: EdgeInsets.only(bottom: 15.sw()),
          child: content,
        ),
      ],
    );
  }

  Widget getItemView(int index, RoleData roleData) {
    if (roleData.type == 0) {
      LogUtils.i("官方角色");
      if (index % 2 == 0) {
        return GestureDetector(
          child: _panelBg(officialItem.getLeftRoleView(roleData)),
          onTap: () {
            _dealWithClickItem(roleData);
          },
        );
      } else {
        return GestureDetector(
          child: _panelBg(officialItem.getRightRoleView(roleData)),
          onTap: () {
            _dealWithClickItem(roleData);
          },
        );
      }
    } else {
      LogUtils.i("自建角色");
      return GestureDetector(
        child: _panelBg(customRoleItem.getCustomRoleView(roleData)),
        onTap: () {
          _dealWithClickItem(roleData);
        },
      );
    }
  }

  void _dealWithClickItem(RoleData roleData) {
    if (UserManager.get().isLogin()) {
      RouteManager.off(RouteConstants.loginPage);
      /*if (roleData.isLock == true) {
        LogUtils.i("跳转到解锁页");
        Map<String, String> params = Map();
        RouteManager.off(RouteConstants.webPage, mParams: params);
      } else {
        LogUtils.i("跳转到聊天页");
      }*/
    } else {
      LogUtils.i("跳转到登录页");
      RouteManager.to(RouteConstants.loginPage);
    }
  }

  void observerToRefreshUI() {
    viewModel?.roleListLd
        .addObserver(LiveListener(change: (List<RoleData>? it) {
      if (it != null) {
        LogUtils.i("监听的数据:" + it.toString());
        roleList = it;
        _refreshUI();
      }
    }));

    LogUtils.i("页面可见，更新数据:");
  }

  void _refreshUI() {
    LogUtils.d("setState刷新UI");
    setState(() {});
  }
}
